*Earnings, CashFlow and Accruals Variables Creation
*Declarating time and firm components
tsset firm year, yearly
*Generating earnings, cashflows and accruals scaled variables
generate sni = ni/asset
generate socf = ocf/asset
generate sacc = (ni - ocf)/asset

*Generating unexpected earnings
generate ux = (ni - L.ni)/L.mcapdec

*Computing firm specific earnings persistence (peroa): computed as the firm-specific angular coefficient, β, of equation 1: X_ti=α+βX_(t-1i)+ε_it
by firm : regress sni L.sni

* Generating decreasing ux
generate negux = 0
replace negux = 1 if ux<0
replace negux = . if missing(ux)

*formally dropping year 2004 used only to compute variations in the first period (ux). 
drop if year==2004

*Winsoring  variables
winsor sni, gen(sniw) p(0.01)
winsor socf, gen(socfw) p(0.01)
winsor sacc, gen(saccw) p(0.01)
winsor ux, gen(uxw) p(0.01)
winsor abret, gen(abretw) p(0.01)


*OPERATING CASH FLOW AND ACCRUAL PERSISTENCE (TABLE 2)
xtreg sniw L.socfw L.saccw, fe vce(robust)
test L.socfw = L.saccw
xtreg sniw L.socfw L.saccw if segcod==1, fe vce(robust)
test L.socfw = L.saccw
xtreg sniw L.socfw L.saccw if segcod==2, fe vce(robust)
test L.socfw = L.saccw
xtreg sniw L.socfw L.saccw if segcod==3, fe vce(robust)
test L.socfw = L.saccw
xtreg sniw L.socfw L.saccw if segcod==4, fe vce(robust)
test L.socfw = L.saccw


*DIAGNOSTICS
xtreg sniw L.socfw L.saccw, fe
estimate store FE
xtreg sniw L.socfw L.saccw, re
estimate store RE
hausman FE RE
xttest0

xtreg sniw L.socfw L.saccw if segcod==1, fe
estimate store FE
xtreg sniw L.socfw L.saccw if segcod==1, re
estimate store RE
hausman FE RE
xttest0

xtreg sniw L.socfw L.saccw if segcod==2, fe
estimate store FE
xtreg sniw L.socfw L.saccw if segcod==2, re
estimate store RE
hausman FE RE
xttest0

xtreg sniw L.socfw L.saccw if segcod==3, fe
estimate store FE
xtreg sniw L.socfw L.saccw if segcod==3, re
estimate store RE
hausman FE RE
xttest0

xtreg sniw L.socfw L.saccw if segcod==4, fe
estimate store FE
xtreg sniw L.socfw L.saccw if segcod==4, re
estimate store RE
hausman FE RE
xttest0


* EARNINGS RESPONSE COEFFICIENT - BASELINE
*TABLE 3 – PANEL A – Base line earnings response coefficient
regress abretw uxw i.year ifrs usgaap i.regcod i.segcod, vce(robust)
regress abretw uxw i.year ifrs usgaap i.regcod i.segcod if segcod==1, vce(robust)
regress abretw uxw i.year ifrs usgaap i.regcod i.segcod if segcod==2, vce(robust)
regress abretw uxw i.year ifrs usgaap i.regcod i.segcod if segcod==3, vce(robust)
regress abretw uxw i.year ifrs usgaap i.regcod i.segcod if segcod==4, vce(robust)

*DIAGNOSTICS
xtreg abretw uxw i.year , fe
estimate store FE
xtreg abretw uxw i.year, re
estimate store RE
hausman FE RE
xttest0

xtreg abretw uxw i.year if segcod==1, fe
estimate store FE
xtreg abretw uxw i.year if segcod==1, re
estimate store RE
hausman FE RE
xttest0

xtreg abretw uxw i.year if segcod==2, fe
estimate store FE
xtreg abretw uxw i.year if segcod==2, re
estimate store RE
hausman FE RE
xttest0

xtreg abretw uxw i.year if segcod==3, fe
estimate store FE
xtreg abretw uxw i.year if segcod==3, re
estimate store RE
hausman FE RE
xttest0

xtreg abretw uxw i.year if segcod==4, fe
estimate store FE
xtreg abretw uxw i.year if segcod==4, re
estimate store RE
hausman FE RE
xttest0

*TABLE 3 – PANEL B – The effect of performance in earnings response coefficient
* Sample split
regress abretw uxw i.year if uxw>0, vce(robust)
regress abretw uxw i.year if uxw<0, vce(robust)
* Interactive regression
regress abretw uxw c.uxw#c.negux negux i.year, vce(robust)

*DIAGNOSTICS
xtreg abretw uxw i.year if uxw>0, fe
estimate store FE
xtreg abretw uxw i.year if uxw>0, re
estimate store RE
hausman FE RE
xttest0

xtreg abretw uxw i.year if uxw<0, fe
estimate store FE
xtreg abretw uxw i.year if uxw<0, re
estimate store RE
hausman FE RE
xttest0

xtreg abretw uxw c.uxw#c.negux negux i.year, fe
estimate store FE
xtreg abretw uxw c.uxw#c.negux negux i.year, re
estimate store RE
hausman FE RE
xttest0



* EARNINGS RESPONSE COEFFICIENT – EXTENDED MODEL
*TABLE 4 – Determinants of Earnings Response Coefficient – Extended Model
regress abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year i.segcod, vce(robust)
regress abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year if segcod==1, vce(robust)
regress abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year if segcod==2, vce(robust)
regress abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year if segcod==3, vce(robust)
regress abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year if segcod==4, vce(robust)


*DIAGNOSTICS
xtreg abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year i.segcod, fe
estimate store FE
xtreg abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year i.segcod, re
estimate store RE
hausman FE RE
xttest0

xtreg abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year if segcod==1, fe
estimate store FE
xtreg abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year if segcod==1, re
estimate store RE
hausman FE RE
xttest0

xtreg abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year if segcod==2, fe
estimate store FE
xtreg abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year if segcod==2, re
estimate store RE
hausman FE RE
xttest0

xtreg abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year if segcod==3, fe
estimate store FE
xtreg abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year if segcod==3, re
estimate store RE
hausman FE RE
xttest0

xtreg abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year if segcod==4, fe
estimate store FE
xtreg abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year if segcod==4, re
estimate store RE
hausman FE RE
xttest0


*MODELS ESTIMATION COMPARISONS 
*1) TABLE 5 – PANEL A - Persistence decomposition 

*Pooled OLD
regress sniw L.socfw L.saccw, vce(robust)
test L.socfw = L.saccw

*Fixed effects
xtreg sniw L.socfw L.saccw, fe vce(robust)
test L.socfw = L.saccw

*Randon Effects
xtreg sniw L.socfw L.saccw, re vce(robust)
test L.socfw = L.saccw

*GLM
glm sniw L.socfw L.saccw, family(gaussian) link(identity) vce(cluster firm)
test L.socfw = L.saccw

*GLM GEE population-averaged model
xtgee sniw L.socfw L.saccw, family(gaussian) link(identity) vce(robust)
test L.socfw = L.saccw



*2) TABLE 5 – PANEL B - Basic ERC and Performance 

*Pooled OLS
regress abretw uxw c.uxw#c.negux negux i.year ifrs usgaap i.regcod i.segcod, vce(robust)

*Fixed effects
xtreg abretw uxw c.uxw#c.negux negux i.year ifrs usgaap i.regcod i.segcod, fe vce(robust)

*Randon Effects
xtreg abretw uxw c.uxw#c.negux negux i.year ifrs usgaap i.regcod i.segcod, re vce(robust)

*GLM
glm abretw uxw c.uxw#c.negux negux i.year ifrs usgaap i.regcod i.segcod, family(gaussian) link(identity) vce(cluster firm)

*GLM GEE population-averaged model
xtgee abretw uxw c.uxw#c.negux negux i.year ifrs usgaap i.regcod i.segcod, family(gaussian) link(identity) vce(robust)



*3) TABLE 5 – PANEL C ERC and Risk Variables 
*Pooled OLS
regress abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year i.segcod, vce(robust)

*Fixed effects
xtreg abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year i.segcod, fe vce(robust)

*Randon Effects
xtreg abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year i.segcod, re vce(robust)

*GLM
glm abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year i.segcod, family(gaussian) link(identity) vce(cluster firm)

*GLM GEE population-averaged model
xtgee abretw uxw c.uxw#c.peroa c.uxw#c.nlef c.uxw#c.beta c.uxw#c.rinslia peroa nlef beta rinslia ifrs usgaap i.regcod i.year i.segcod, family(gaussian) link(identity) vce(robust)




